// GateServer project main.go

package main

import (
    "../pkg/log"
    //"./network"
    "../pkg/unit"
    "flag"
    "fmt"
    //gocfg "github.com/kless/goconfig/config"
    gocfg "github.com/robfig/config"
    "runtime"
)

func main() {
    config_file := flag.String("conf", "gate.cfg",
        "gateserver starup config file")
    flag.Parse()

    cfg, _ := gocfg.ReadDefault(*config_file)
    maxprocs, _ := cfg.Int("runtime", "maxprocs")
    loglv, _ := cfg.Int("runtime", "loglv")
    //maxonline, _ := cfg.Int("runtime", "maxonline")
    runtime.GOMAXPROCS(maxprocs)

    world_listen_name, _ := cfg.String("world_listen", "name")
    world_listen_ip, _ := cfg.String("world_listen", "ip")
    world_listen_port, _ := cfg.Int("world_listen", "port")
    world_listen_rcvbuf, _ := cfg.Int("world_listen", "rcvbuf")
    world_listen_sndbuf, _ := cfg.Int("world_listen", "sndbuf")
    world_listen_sndchan, _ := cfg.Int("world_listen", "sndchan")
    world_listen_maxconn, _ := cfg.Int("world_listen", "maxconn")
    world_listen_maxidle, _ := cfg.Int("world_listen", "maxidle")

    gate_listen_name, _ := cfg.String("gate_listen", "name")
    gate_listen_ip, _ := cfg.String("gate_listen", "ip")
    gate_listen_port, _ := cfg.Int("gate_listen", "port")
    gate_listen_rcvbuf, _ := cfg.Int("gate_listen", "rcvbuf")
    gate_listen_sndbuf, _ := cfg.Int("gate_listen", "sndbuf")
    gate_listen_sndchan, _ := cfg.Int("gate_listen", "sndchan")
    gate_listen_maxconn, _ := cfg.Int("gate_listen", "maxconn")
    gate_listen_maxidle, _ := cfg.Int("gate_listen", "maxidle")

    fmt.Println("runtime\t\tvalue")
    fmt.Println("-----------------------------------------------------------------------")
    fmt.Printf("%v\t%v\n", "maxprocs", maxprocs)
    fmt.Printf("%v\t\t%v\n\n", "loglv", loglv)

    fmt.Println("info\tip\t\t\tport\tmaxconn\tmaxidle\trcvbuf\tsndbuf\tsndchan")
    fmt.Println("-----------------------------------------------------------------------")
    fmt.Printf("%v\t%v\t%v\t%v\t\t%v\t%vK\t%vK\t%v\n", world_listen_name, world_listen_ip,
        world_listen_port, world_listen_maxconn, world_listen_maxidle,
        world_listen_rcvbuf/1024, world_listen_sndbuf/1024, world_listen_sndchan)
    fmt.Printf("%v\t%v\t%v\t%v\t\t%v\t%vK\t%vK\t%v\n\n", gate_listen_name, gate_listen_ip,
        gate_listen_port, gate_listen_maxconn, gate_listen_maxidle,
        gate_listen_rcvbuf/1024, gate_listen_sndbuf/1024, gate_listen_sndchan)

    log := log.NewMyLog(loglv)

    //evp := network.NewEventProcessor()
    world_service := unit.NewWorldService(world_listen_name, log, nil,
        (uint64)(world_listen_maxconn), world_listen_rcvbuf, world_listen_sndbuf, world_listen_sndchan)
    gate_service := unit.NewGateService(gate_listen_name, log, nil,
        (uint64)(gate_listen_maxconn), gate_listen_rcvbuf, gate_listen_sndbuf, gate_listen_sndchan)

    unit.NewGlobal(gate_service)

    err := world_service.Run("0.0.0.0", world_listen_port, (int64)(world_listen_maxidle))
    if err != nil {
        log.Error("system", "Starup GateServer listener failed:%v", err)
        return
    }
    log.Success("system", "Starup World listener succeed!")

    err = gate_service.Run("0.0.0.0", gate_listen_port, (int64)(gate_listen_maxidle))
    if err != nil {
        log.Error("system", "Starup GateServer listener failed:%v", err)
        return
    }
    log.Success("system", "Starup GateServer listener succeed!")
    world_service.Join()
    gate_service.Join()
}
